package com.lhd.usermanagement.exception;

import com.lhd.usermanagement.common.BaseResponse;
import com.lhd.usermanagement.common.BaseResultUtils;
import com.lhd.usermanagement.common.ErrorCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(BusinessException.class)
    public BaseResponse businessExceptionHandler(BusinessException e) {
        log.error("businessExceptionHandler",e.getMessage());
        return BaseResultUtils.error(e.getCode(),e.getMessage(),e.getDescription());
    }
    @ExceptionHandler(RuntimeException.class)
    public BaseResponse runtimeExceptionHandler(RuntimeException e) {
        log.error("runtimeExceptionHandler",e.getMessage());
        return BaseResultUtils.error(ErrorCode.SYSTEM_ERROR,e.getMessage(),"");
    }

    /**
     * 处理HttpMediaTypeNotAcceptableException
     * @param e HttpMediaTypeNotAcceptableException
     * @return 返回一个适配的错误响应
     */
    @ExceptionHandler(HttpMediaTypeNotAcceptableException.class)
    public BaseResponse mediaTypeNotAcceptableExceptionHandler(HttpMediaTypeNotAcceptableException e) {
        log.error("mediaTypeNotAcceptableExceptionHandler", e.getMessage());
        return BaseResultUtils.error(ErrorCode.SERVICES_INCOMPATIBLE, "不支持的媒体类型", "");
    }
}
